<?php
/**
 * 取_中国钢铁现货网_数据
 */
$company_all_gtxh = array();

$body = file_get_contents("http://steel.gtxh.com/Search/SearchData?pidx=1");
preg_match_all('/<tr class=(.+?)<\/tr>/s',$body,$arr);

foreach ($arr[1] as $value) {
	preg_match('/cid=\'(.+)\'>/', $value, $comapny_id);//公司ID
	
	$company = get_comapny(trim($comapny_id[1]) ,&$company_all_gtxh);
/* 
 * 公司的格式
	stdClass Object
	(
			[CompanyName] => 常州市广源金属材料有限公司
			[Adress] => -
			[ConMan] => 周女士
			[ConPhone] => 051988257319
			[Mob] => 13601507180
			[Bs] =>
	)
	 */
	preg_match_all('/<td>(.+?)<\/td>/s',$value,$td);
	
	$name = trim(strip_tags($td[1][0]));//品名
	$specification = trim(trim(strip_tags($td[1][1])),"…");//规格
	$material = trim(strip_tags($td[1][2]));//材质
	$factory = trim(strip_tags($td[1][3]));//钢厂
	$city = trim(strip_tags($td[1][4]));//存货区,省
	$warehouse = trim(strip_tags($td[1][5]));//仓库
	
	$price5 = trim(strip_tags($td[1][6]));
	$price = is_numeric($price5)?$price5:"面议";//价格
	
	$qty = trim(strip_tags($td[1][7]));//吨位
	$time = trim($td[1][8]);//更新时间
	
	echo "comapny_id:$company->CompanyName,name:$name,specification:$specification,material:$material,factory:$factory,city:$city,price:$price,qty:$qty,time:$time\r\n";
}


/**
 * 取得公司的信息
 * @param 公司id $company_id
 * @param 已存在的公司 $company_all_gtxh
 * @return 公司数组
 */
function get_comapny($company_id, $company_all_gtxh){
	if(empty($company_all_gtxh[$company_id])){
		$company = file_get_contents("http://steel.gtxh.com/CompanyImfor/CardImfor?cid=$company_id");
		$company_array = json_decode($company);
		$company_all_gtxh[$company_id] = $company_array;
	}
	return $company_all_gtxh[$company_id];
}
